/******************************************************************************
 *	@Author Raffaello Brondi
 *
 * 	@description Loading all the FB script files
 *
 ******************************************************************************/
 
(function(){
	var parent = getScriptTag().parentNode;
	//loadFB(parent);
	//loadFBAll(parent);
	loadPermissionsJS(parent);
}());

function loadPermissionsJS(parent) {
	var location = _fxb_domain + 'fb/permissions.js';
	parent.appendChild(newScriptElement(location, true, function() {
		loadUserJS(parent);
	}));
}

function loadUserJS(parent) {
	var location = _fxb_domain + 'fb/user.js';
	parent.appendChild(newScriptElement(location, true, function() {
		get_fb_JS(parent);
	}));
}


function get_fb_JS(parent){
	var location = _fxb_domain + 'fb/main.js'; 
	parent.appendChild(newScriptElement(location, true, function() {
		get_facebook_API_JS(parent);
	}));
}

function get_facebook_API_JS(parent){
	var location = 'https://connect.facebook.net/en_US/all.js'; 
	parent.appendChild(newScriptElement(location, true, function() {
		initFB();
		if(typeof(window.on_fb_load_completed) == 'function')
			window.on_fb_load_completed();
			
	}));
}

function newScriptElement(src_loc, async, onComplete) {
	var script = document.createElement('script');
	script.src = src_loc;
	if(async != null)
		script.async = async;
	script.setAttribute("type", "text/javascript");

	if(onComplete){		
		script.onload = onComplete; //for gecko browser
		script.onreadystatechange = function(){ //for IE
			if (this.readyState == 'complete'){
				onComplete();
			}
		}
	}
	return script;
}
